package com.wwdeng.smart.video

import android.annotation.SuppressLint
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.wwdeng.smart.base.BaseFm
import com.wwdeng.smart.databinding.IncludeVideoRecordingBinding
import com.wwdeng.smart.video.adapter.VideoRecordAdapter
import org.json.JSONObject

/**
 * android
 * @Author：wwdeng
 * @CreateTime：2024/9/16 17:35
 * <pre>
 * 回放视频操作
 * </pre>
 */
class RecordingFragment(): BaseFm() {
    private lateinit var binding: IncludeVideoRecordingBinding
    private var videoAct: VideoPreviewAct? = null
    private var videoList: ArrayList<JSONObject>? = null

    private var videoAdapter: VideoRecordAdapter? = null

    constructor(videoAct: VideoPreviewAct, videoList: ArrayList<JSONObject>): this(){
        this.videoAct = videoAct
        this.videoList = videoList
    }

    override fun getRootView(): View {
        binding = IncludeVideoRecordingBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initView() {
        initRecyclerView()
    }

    override fun initListener() {
    }

    override fun initData() {
    }

    /**
     * 初始化视频列表
     */
    @SuppressLint("NotifyDataSetChanged")
    private fun initRecyclerView(){
        binding.recyclerView.setHasFixedSize(true)
        binding.recyclerView.isNestedScrollingEnabled = false
        binding.recyclerView.isFocusableInTouchMode = false //不需要焦点
        val layoutManager = LinearLayoutManager(context)
        binding.recyclerView.layoutManager = layoutManager
        videoAdapter = VideoRecordAdapter(binding.recyclerView)
        videoAdapter!!.setOnItemChildClickListener { _, _, position ->
            val item = this.videoAdapter!!.getItem(position)
            if (item.optBoolean("isSelect",false)){
                return@setOnItemChildClickListener
            }
            for (i in 0 until videoAdapter!!.itemCount){
                val obj = videoAdapter!!.getItem(i)
                obj.put("isSelect",false)
                if (i == position){
                    obj.put("isSelect",true)

                }
            }
            videoAdapter!!.notifyDataSetChanged()
        }
        binding.recyclerView.adapter = videoAdapter
        videoAdapter!!.data = videoList
    }

}